package weka;


import model.MyAttribute;
import model.MyInstances;
import weka.core.*;
import weka.core.converters.ConverterUtils;
import java.util.ArrayList;

/**
 * Created by taohj on 2014/12/19.
 */
public class CommonUtils {

    public static Instances getInstances(String fileRealPath) throws Exception {
        ConverterUtils.DataSource source = new ConverterUtils.DataSource(fileRealPath);
        Instances instances = source.getDataSet();

        return instances;

    }

    public static MyInstances getMyInstances(Instances instances) throws Exception {
        if (instances == null) {
            throw new Exception("instances 不能是空的");
        }
        MyInstances myInstances = new MyInstances();
        myInstances.setNumAttributes(instances.numAttributes());
        myInstances.setNumInstances(instances.numInstances());
        myInstances.setRelationName(instances.relationName());

        myInstances.setAttributes(new ArrayList<MyAttribute>());
        for (int i = 0; i < instances.numAttributes(); i++) {
            MyAttribute myattribute = new MyAttribute();
            myattribute.setId(i);
            myattribute.setName(instances.attribute(i).name());
            myInstances.getAttributes().add(myattribute);
        }
        return myInstances;
    }



}
